import { getUserList, info } from "@/services/user";
import { setAuthority } from "@/utils/authority";
import { getPageQuery } from "@/utils/utils";
import { reloadAuthorized } from "@/utils/Authorized";
import { routerRedux } from "dva/router";
import { stringify } from "qs";
import UserSession from "@/UserSession";
import { router } from "umi";
import { message } from "antd/lib/index";
import { notification, Icon } from "antd";
import moment from "moment";

export default {
	namespace: "user",
	state: {
		currentUser: {},
	},

	effects: {
		*fetchCurrent({ payload }, { call, put }) {
			const response = yield call(getUserList, payload);
			if (response) {
				yield put({
					type: "saveCurrentUser",
					payload: response.list[0],
				});
			}
		},
		*getCurrent({ payload }, { call, put }) {
			const response = yield call(info, payload);
			window.localStorage.setItem(
				"currentUser",
				JSON.stringify({
					...response,
					activeSystem: payload.activeSystem,
				})
			);
			if (response) {
				yield put({
					type: "saveCurrentUser",
					payload: {
						...response,
						activeSystem: payload.activeSystem,
					},
				});
				reloadAuthorized();
			}
		},

		*changeSystemMenu({ payload }, { call, put }) {
			if (UserSession.getAccountNonExpired()) {
				if (payload != "homeCenter" && payload != "interactCenter" && !UserSession.canJumpToOtherMenu()) {
					return;
				};
				UserSession.setActiveMenu(payload);
				yield put({
					type: "updateState",
					payload: {
						currentAuthority: UserSession.getSystemMenu()[payload],
					},
				});
				reloadAuthorized();
				if (payload === "interactCenter") {
					return router.push("/homeType");
				};
				if (payload === "homeCenter") {
					return router.push("/home");
				};
				
				window.history.pushState(null, null, "/?s=" + payload);
				window.location.reload(false);
			} else {
				message.info("账户已经过期，将为您跳转到登录页");
				UserSession.clearCache();
				yield put(routerRedux.push("/user/login"));
			}
		},
	},
	subscriptions: {
		setup({ dispatch }) {
			const data = JSON.parse(localStorage.getItem("currentUser"));
			if (!data) {
				dispatch({
					type: "saveCurrentUser",
					payload: {},
				});
			} else {
				dispatch({
					type: "saveCurrentUser",
					payload: data,
				});
			}
		},
		setupHistory({ dispatch, history }) {
			history.listen((location) => {
				if (location.pathname === "/user/login") {
					dispatch({
						type: "saveCurrentUser",
						payload: {},
					});
				}
			});
		},
	},
	reducers: {
		updateState(state, { payload }) {
			console.log(state, payload);
			setAuthority(payload.currentAuthority);
			return {
				...state,
				...payload,
			};
		},
		saveCurrentUser(state, action) {
			const activeMenu = UserSession.getActiveMenu();
			const menu = UserSession.getSystemMenu();
			if (menu) {
				setAuthority(menu[activeMenu]);
			}

			return {
				...state,
				currentUser: action.payload || {},
			};
		},
		changeNotifyCount(state, action) {
			return {
				...state,
				currentUser: {
					...state.currentUser,
					notifyCount: action.payload.totalCount,
					unreadCount: action.payload.unreadCount,
				},
			};
		},
	},
};
